/* USER CODE BEGIN Header */
/**
  ******************************************************************************
  * File Name          : freertos.c
  * Description        : Code for freertos applications
  ******************************************************************************
  * @attention
  *
  * Copyright (c) 2025 STMicroelectronics.
  * All rights reserved.
  *
  * This software is licensed under terms that can be found in the LICENSE file
  * in the root directory of this software component.
  * If no LICENSE file comes with this software, it is provided AS-IS.
  *
  ******************************************************************************
  */
/* USER CODE END Header */

/* Includes ------------------------------------------------------------------*/
#include "FreeRTOS.h"
#include "task.h"
#include "main.h"
#include "cmsis_os.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "ili9488_spi.h"
#include "lcd.h"
#include "gui.h"
#include "test.h"
#include "DS_18B20.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"

#include "ESP8266.h"

#include <elog.h>
#define LOG_TAG_TEMP "TEMP"
#define LOG_TAG_FAN  "FAN"

#include "MyFlash.h"

//#include "onenet.h"
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
			char buffer[16];
			uint8_t Fengshan=0;
			float Temper=0.0;
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */

/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/////////////////////////////////ESP8266
#define RXBUFFERSIZE 256      
char send_buffer[128];       
char RxBuffer[RXBUFFERSIZE]; 
uint8_t aRxBuffer;           
uint8_t Uart3_Rx_Cnt = 0;     
char my_order[15] = {0};      
char receive_flag = 0;        
/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN Variables */
void test_elog(void) {
    /* test log output for all level */
    log_a("Hello EasyLogger!");
    log_e("Hello EasyLogger!");
    log_w("Hello EasyLogger!");
    log_i("Hello EasyLogger!");
    log_d("Hello EasyLogger!");
    log_v("Hello EasyLogger!");
//    elog_raw("Hello EasyLogger!");
}

/* USER CODE END Variables */
osThreadId defaultTaskHandle;
osThreadId DS18B20Task02Handle;
osThreadId LCDTask03Handle;
osThreadId PWMTask04Handle;
osThreadId WIFITask05Handle;
osMessageQId TemperHandle;

/* Private function prototypes -----------------------------------------------*/
/* USER CODE BEGIN FunctionPrototypes */

/* USER CODE END FunctionPrototypes */

void StartDefaultTask(void const * argument);
void StartTaskDS18B20(void const * argument);
void StartTaskLCD(void const * argument);
void StartTaskPWM(void const * argument);
void StartTaskWIFI(void const * argument);

void MX_FREERTOS_Init(void); /* (MISRA C 2004 rule 8.1) */

/* GetIdleTaskMemory prototype (linked to static allocation support) */
void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize );

/* USER CODE BEGIN GET_IDLE_TASK_MEMORY */
static StaticTask_t xIdleTaskTCBBuffer;
static StackType_t xIdleStack[configMINIMAL_STACK_SIZE];

void vApplicationGetIdleTaskMemory( StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize )
{
  *ppxIdleTaskTCBBuffer = &xIdleTaskTCBBuffer;
  *ppxIdleTaskStackBuffer = &xIdleStack[0];
  *pulIdleTaskStackSize = configMINIMAL_STACK_SIZE;
  /* place for user code */
}
/* USER CODE END GET_IDLE_TASK_MEMORY */

/**
  * @brief  FreeRTOS initialization
  * @param  None
  * @retval None
  */
void MX_FREERTOS_Init(void) {
  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* USER CODE BEGIN RTOS_MUTEX */
  /* add mutexes, ... */
  /* USER CODE END RTOS_MUTEX */

  /* USER CODE BEGIN RTOS_SEMAPHORES */
  /* add semaphores, ... */
  /* USER CODE END RTOS_SEMAPHORES */

  /* USER CODE BEGIN RTOS_TIMERS */
  /* start timers, add new ones, ... */
  /* USER CODE END RTOS_TIMERS */

  /* Create the queue(s) */
  /* definition and creation of Temper */
  osMessageQDef(Temper, 16, uint16_t);
  TemperHandle = osMessageCreate(osMessageQ(Temper), NULL);

  /* USER CODE BEGIN RTOS_QUEUES */
  /* add queues, ... */
  /* USER CODE END RTOS_QUEUES */

  /* Create the thread(s) */
  /* definition and creation of defaultTask */
  osThreadDef(defaultTask, StartDefaultTask, osPriorityNormal, 0, 128);
  defaultTaskHandle = osThreadCreate(osThread(defaultTask), NULL);

  /* definition and creation of DS18B20Task02 */
  osThreadDef(DS18B20Task02, StartTaskDS18B20, osPriorityHigh, 0, 128);
  DS18B20Task02Handle = osThreadCreate(osThread(DS18B20Task02), NULL);

  /* definition and creation of LCDTask03 */
  osThreadDef(LCDTask03, StartTaskLCD, osPriorityIdle, 0, 128);
  LCDTask03Handle = osThreadCreate(osThread(LCDTask03), NULL);

  /* definition and creation of PWMTask04 */
  osThreadDef(PWMTask04, StartTaskPWM, osPriorityIdle, 0, 128);
  PWMTask04Handle = osThreadCreate(osThread(PWMTask04), NULL);

  /* definition and creation of WIFITask05 */
  osThreadDef(WIFITask05, StartTaskWIFI, osPriorityIdle, 0, 128);
  WIFITask05Handle = osThreadCreate(osThread(WIFITask05), NULL);

  /* USER CODE BEGIN RTOS_THREADS */
  /* add threads, ... */
  /* USER CODE END RTOS_THREADS */

}

/* USER CODE BEGIN Header_StartDefaultTask */
/**
  * @brief  Function implementing the defaultTask thread.
  * @param  argument: Not used
  * @retval None
  */
/* USER CODE END Header_StartDefaultTask */
void StartDefaultTask(void const * argument)
{
  /* USER CODE BEGIN StartDefaultTask */
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartDefaultTask */
}

/* USER CODE BEGIN Header_StartTaskDS18B20 */
/**
* @brief Function implementing the DS18B20Task02 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskDS18B20 */
void StartTaskDS18B20(void const * argument)
{
  /* USER CODE BEGIN StartTaskDS18B20 */
	 int i,j=0;
	 ds18b20_init();
	 uint32_t writeData[DATA_SIZE/4];
   uint32_t readData[DATA_SIZE/4];
  /* Infinite loop */
  for(;;)
  {
		 Temper = ds18b20_read();
		 sprintf(buffer, "%.2fC", Temper);
		if(Temper>30)
		{
				HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
			  __HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 10);
//				printf("Temper = %.2f high\r\n",Temper);
			  Fengshan=2;
			  log_i("Fan high");
			  
		}
		else if(Temper>28&&Temper<30)
		{
				HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
				__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 500);
		    Fengshan=1;
			  log_i("Fan low");
//				printf("Temper = %.2f low\r\n",Temper);
		}
		else if(Temper<28)
		{
				HAL_TIM_PWM_Stop(&htim3,TIM_CHANNEL_1);
//				printf("Temper = %.2f stop\r\n",Temper);
			  Fengshan=0;
//			  log_i("log_i Fan stop");
				#define LOG_TAG  "FAN"
			  log_a("Fan stop");
		}
		#define LOG_TAG  "DS18B20"
		log_i("Temper = %.2f C",Temper);  

    osDelay(2000);
  }
  /* USER CODE END StartTaskDS18B20 */
}

/* USER CODE BEGIN Header_StartTaskLCD */
/**
* @brief Function implementing the LCDTask03 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskLCD */
void StartTaskLCD(void const * argument)
{
  /* USER CODE BEGIN StartTaskLCD */
	LCD_Init();
		float Temper_Flash=5.24;
	uint32_t tempData;
  uint32_t readData; // ��ȡ��֤
  float readTemp;
    memcpy(&tempData, &Temper_Flash, sizeof(float)); 
    Flash_EraseSector(FLASH_USER_SECTOR);
    Flash_Write(FLASH_USER_START_ADDR, &tempData, sizeof(tempData)); 
    Flash_Read(FLASH_USER_START_ADDR, &readData, sizeof(readData));
    memcpy(&readTemp, &readData, sizeof(float)); 
//  printf("\nRaw HEX: 0x%08X", readData);
    printf("  Flah_readTemp: %.2f��C\r\n", readTemp);

  /* Infinite loop */
  for(;;)
  {
		main_test();
    osDelay(1000);
  }
  /* USER CODE END StartTaskLCD */
}

/* USER CODE BEGIN Header_StartTaskPWM */
/**
* @brief Function implementing the PWMTask04 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskPWM */
void StartTaskPWM(void const * argument)
{
  /* USER CODE BEGIN StartTaskPWM */
		elog_init();
	/* set EasyLogger log format */
	elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_ALL& ~( ELOG_FMT_T_INFO | ELOG_FMT_P_INFO));
	elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
	elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~(ELOG_FMT_FUNC | ELOG_FMT_T_INFO | ELOG_FMT_P_INFO));
	elog_start();
	
	HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_1);
  /* Infinite loop */
  for(;;)
  {
    osDelay(1);
  }
  /* USER CODE END StartTaskPWM */
}

/* USER CODE BEGIN Header_StartTaskWIFI */
/**
* @brief Function implementing the WIFITask05 thread.
* @param argument: Not used
* @retval None
*/
/* USER CODE END Header_StartTaskWIFI */
void StartTaskWIFI(void const * argument)
{
  /* USER CODE BEGIN StartTaskWIFI */
	  ESP8266_Kingy();
//中文乱码测试中文乱码测试中文乱码测试中文乱码测试中文乱码测试中文乱码测试
  /* Infinite loop */
  for(;;)
  {
if(receive_flag) 
	   {
         receive_flag = 0;
        if(strstr(my_order, "<on>")) {
        printf("ononon!!!\r\n");
      } else if(strstr(my_order, "<off>")) {
       printf("offoffoff!!!\r\n");
      }
       Uart3_Rx_Cnt = 0;
       memset(RxBuffer, 0, sizeof(RxBuffer));
       memset(my_order, 0, sizeof(my_order));
   }

            snprintf(send_buffer, sizeof(send_buffer), "Temp: %.2fC\r\n", Temper);
            HAL_Delay(1000); 
            ESP8266_SendData(0,send_buffer, 1000);
    osDelay(2000);
  }
  /* USER CODE END StartTaskWIFI */
}

/* Private application code --------------------------------------------------*/
/* USER CODE BEGIN Application */

/* USER CODE END Application */
